package com.itheima.proxy;

import com.itheima.advice.Myadvice;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//测试类
public class Myproxy {
    public static void main(String[] args) {
        final UserServiceImpl userService =new UserServiceImpl();//真实类
        final Myadvice myadvice=new Myadvice();//通知类
        //创建代理对象
        UserService o = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(),
                userService.getClass().getInterfaces(),
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                 if (method.getName().equals("showMany")){
                    Double money = (Double) args[0];
                    if (money>10000){
                       method.invoke(userService, money *0.8);
                       myadvice.yqr();
                    }else {
                        method.invoke(userService, args);
                        myadvice.qr();
                    }

                 }


                        return null;
                    }
                }
        );

o.showMany(111111);
    }
}
